package com.kecheng.action;

import com.kecheng.dao.CommonDao;
import com.kecheng.dao.TeamDao;
import com.kecheng.moban.PageBean;
import com.kecheng.pojo.Team;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.AllowedMethods;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@ParentPackage("json-default")
@Action(value = "team", results = {
		@Result(name = "studentList", location = "/admin/student/studentList.jsp"),
		@Result(name = "toDetail", location = "/admin/student/studentDetail.jsp"),
		@Result(name = "redirectList", type = "redirect",location = "team!studentList.action"),
		@Result(name = "save", type = "json",params = {"root","student"})
})
@AllowedMethods(value = {("regex:.*")})
public class TeamAction extends BaseAction {
	@Autowired
	TeamDao teamDao;
	@Autowired
	CommonDao dao;
	private PageBean<Team> pageBean;
	private Team student;
	private List<Integer> id;
	private String searchVal;
	/**
	 * 列表
	 * @return
	 */
	public String studentList(){
		pageBean = (PageBean<Team>) dao.findPageBean(Team.class,pageBean);
		return "studentList";
	}
	/**
	 *详情页
	 * @return
	 */
	public String toDetail(){
		if(student!=null)student = (Team)dao.findOne(student);
		return "toDetail";
	}

	/**
	 * 更新/修改
	 */
	@Transactional(readOnly = false)
	public String save(){
		Team student1 = new Team();
		student1.setId(student.getId());
		student1 = (Team)dao.findOne(student1);
		student1.setName(student.getName());
		student1.setAddress(student.getAddress());
		student1.setPhone(student.getPhone());
		dao.update(student1);
		return "save";
	}

	/**
	 *删除
	 * @return
	 */
	@Transactional(readOnly = false)
	public String delete(){
		if(student!=null)dao.delete(student);
		else if(id!=null){
			for(Integer i:id){
				student = new Team();
				student.setId(i);
				dao.delete(student);
			}
		}
		return "redirectList";
	}

	/**
	 * 搜索
	 * @return
	 */
	public String search(){
		pageBean = teamDao.search(searchVal);
		return "studentList";
	}

	public PageBean<Team> getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean<Team> pageBean) {
		this.pageBean = pageBean;
	}

	public Team getStudent() {
		return student;
	}

	public void setStudent(Team student) {
		this.student = student;
	}

	public List<Integer> getId() {
		return id;
	}

	public void setId(List<Integer> id) {
		this.id = id;
	}

	public String getSearchVal() {
		return searchVal;
	}

	public void setSearchVal(String searchVal) {
		this.searchVal = searchVal;
	}
}
